home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / TTPictures.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  6.5 KB  |  309 lines  |  [TEXT/KAHL]

  1. /* TTPictures.c
  2.  * Display TeachText style pictures in Writeswell Jr.
  3.  * ©1993 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 25 Feb 93 Mike Crawford
  14.  */
  15.  
  16. #include "AppleEvents.h"
  17. #include "TBConstants.h"
  18. #include "TBGlobals.h"
  19. #include "TestBed.h"
  20. #include "TTPictures.h"
  21. #include "MyTextEdit.h"
  22.  
  23. void ShowDocPictures( TEHandle textH, short numPictures );
  24. void DrawAllPictures( TEHandle textH, short numPictures );
  25. void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum );
  26. short GetCharLine( TEHandle textH, short charNum );
  27.  
  28. void ShowPictures( WindowPtr docWindow, short numPictures, short resRefNum )
  29. {
  30.     short        curFile;
  31.     TEHandle    textH;
  32.     GrafPtr        curPort;
  33.  
  34.     if ( !numPictures )
  35.         return;
  36.     
  37.     if ( resRefNum == -1 )
  38.         return;
  39.  
  40.     curFile = CurResFile();
  41.     UseResFile( resRefNum );
  42.     
  43.     GetPort( &curPort );
  44.     SetPort( docWindow );
  45.     
  46.     textH = GetTextHandle( docWindow );
  47.  
  48.     ShowDocPictures( textH, numPictures );
  49.  
  50.     SetPort( curPort );
  51.  
  52.     UseResFile( curFile );
  53.     
  54.     return;
  55. }
  56.  
  57. void ShowDocPictures( TEHandle textH, short numPictures )
  58. {
  59.     RgnHandle        oldClip;
  60.     RgnHandle        newClip;
  61.     RgnHandle        viewRgn;
  62.     Rect            viewRect;
  63.     
  64.     /* Set the clip region to the intersection of the existing clip region
  65.      * and view rectangle (so that we don't draw over the scroll bar).
  66.      * Then draw all the pictures.
  67.      *
  68.      * We put the loop to do all the drawing in a separate function so that
  69.      * we only need one instance of the code where we restore the clip region.
  70.      */
  71.  
  72.     viewRect = (*textH)->viewRect;
  73.     
  74.     oldClip = NewRgn();
  75.     if (!oldClip )
  76.         return;
  77.     
  78.     newClip = NewRgn();
  79.     if (!newClip ){
  80.         DisposeRgn( oldClip );
  81.         return;
  82.     }
  83.     
  84.     viewRgn = NewRgn();
  85.     if (!viewRgn ){
  86.         DisposeRgn( oldClip );
  87.         DisposeRgn( newClip );
  88.         return;
  89.     }
  90.     
  91.     GetClip( oldClip );                        /* Save the original clip region */
  92.  
  93.     GetClip( newClip );                        /* Get the original clip region to work on */
  94.     
  95.     RectRgn( viewRgn, &viewRect );
  96.     
  97.     SectRgn( newClip, viewRgn, newClip );    /* Get the intersection with the viewRect */
  98.     
  99.     SetClip( newClip );
  100.     
  101.     DisposeRgn( newClip );                    /* Toss now-unneeded region handles */
  102.     DisposeRgn( viewRgn );
  103.     
  104.     DrawAllPictures( textH, numPictures );
  105.  
  106.     SetClip( oldClip );                        /* Restore the original clip region */
  107.  
  108.     DisposeRgn( oldClip );
  109.  
  110.     return;
  111. }
  112.  
  113. void DrawAllPictures( TEHandle textH, short numPictures )
  114. {
  115.     CharsHandle        charH;
  116.     unsigned char    *charP;
  117.     short            charNum;
  118.     short            picNum;
  119.     short            textLen;
  120.     short            lastChar;
  121.     short            bottomChar;
  122.     PicHandle        picH;
  123.     Rect            viewRect;
  124.     Point            bottomPoint;
  125.  
  126.     charH = TEGetText( textH );
  127.     
  128.     textLen = (*textH)->teLength;
  129.         
  130.     viewRect = (*textH)->viewRect;
  131.     
  132.     bottomPoint.h = viewRect.right;
  133.     bottomPoint.v = viewRect.bottom;
  134.     
  135.     bottomChar = TEGetOffset( bottomPoint, textH );
  136.     
  137.     if ( bottomChar < textLen )
  138.         lastChar = bottomChar;
  139.     else
  140.         lastChar = textLen;
  141.  
  142.     charNum = 0;
  143.     
  144.     picNum = 0;
  145.     
  146.     charP = (unsigned char*)*charH;                /* Warning: deref unlock handle */
  147.     
  148.     for ( charNum = 0; charNum < lastChar; charNum++ ){
  149.     
  150.         /* Display a picture at each non-breaking space */
  151.  
  152.         if ( *charP++ == 0xca ){
  153.             
  154.             picH = (PicHandle)Get1Resource( 'PICT', 1000 + picNum );
  155.             if ( !picH ){
  156.                 return;
  157.             }
  158.             
  159.             picNum++;
  160.  
  161.             DrawOnePicture( picH, textH, charNum );
  162.             
  163.             if ( picNum >= numPictures ){
  164.                 return;
  165.             }
  166.  
  167.             /* Refresh the character pointer, since its handle may have moved */
  168.             
  169.             charP = (unsigned char*)*charH + charNum + 1;
  170.         }
  171.     }
  172.  
  173.     return;
  174. }
  175.  
  176. void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum )
  177. {
  178.     Rect    dispRect;
  179.     short    picHeight;
  180.     short    picWidth;
  181.     Rect    viewRect;
  182.     short    viewWidth;
  183.     Point    charPoint;
  184.  
  185.     charPoint = TEGetPoint( charNum, textH );
  186.  
  187.     dispRect = (*picH)->picFrame;
  188.     
  189.     picHeight = dispRect.bottom - dispRect.top;
  190.     
  191.     dispRect.top = charPoint.v;
  192.  
  193.     dispRect.bottom = dispRect.top + picHeight;
  194.     
  195.     picWidth = dispRect.right - dispRect.left;
  196.     
  197.     viewRect = (*textH)->viewRect;
  198.  
  199.     viewWidth = viewRect.right - viewRect.left;
  200.  
  201.     dispRect.left = viewRect.left + ( ( viewWidth - picWidth ) / 2 );    /* 1.1.1 MDC Center picture */
  202.     
  203.     dispRect.right = dispRect.left + picWidth;
  204.     
  205.     DrawPicture( picH, &dispRect );
  206.  
  207.     return;
  208. }
  209.  
  210. void ScrollPictures( short dV, TEHandle textH )
  211. {
  212.     RgnHandle    oldClip;
  213.     Rect        scrollClipRect;
  214.  
  215.     /* 2.0 Display the pictures.
  216.      * A side effect of TEScroll is that it will scroll the picture as well - it
  217.      * actually uses ScrollBits.  So we only need to redisplay the part of the
  218.      * picture that has just come into view.
  219.      */
  220.  
  221.     
  222.     oldClip = NewRgn();
  223.     
  224.     if ( !oldClip )
  225.         return;
  226.     
  227.     GetClip( oldClip );
  228.     
  229.     scrollClipRect = (*textH)->viewRect;
  230.     
  231.     if ( dV > 0 ){
  232.     
  233.         /* We are scrolling downward - increasing y values are lower */
  234.  
  235.         scrollClipRect.bottom = scrollClipRect.top + dV;
  236.         
  237.     }else{
  238.     
  239.         /* We are scrolling upward */
  240.     
  241.         scrollClipRect.top = scrollClipRect.bottom + dV;    /* Plus 'cause dV is negative */
  242.     }
  243.  
  244.     ClipRect( &scrollClipRect );
  245.  
  246.     ShowPictures( gScrollWindow, gNumPictures, gResRefNum );
  247.     
  248.     SetClip( oldClip );
  249.  
  250.     DisposeRgn( oldClip );
  251.  
  252.     return;
  253. }
  254. #ifdef NEVER
  255. void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum )
  256. {
  257.     Rect    dispRect;
  258.     Rect    viewRect;
  259.     Rect    destRect;
  260.     short    charLine;
  261.     short    picHeight;
  262.     short    picWidth;
  263.     short    charHeight;
  264.     
  265.     charLine = GetCharLine( textH, charNum );
  266.     
  267.     charHeight = TEGetHeight( charLine, 1, textH );
  268.  
  269.     dispRect = (*picH)->picFrame;
  270.     
  271.     picHeight = dispRect.bottom - dispRect.top;
  272.     
  273.     viewRect = (*textH)->viewRect;
  274.     destRect = (*textH)->destRect;
  275.  
  276.     dispRect.top = destRect.top - viewRect.top + charHeight;
  277.     dispRect.bottom = dispRect.top + picHeight;
  278.     
  279.     picWidth = dispRect.right - dispRect.left;
  280.     
  281.     dispRect.left = kTextInset;        /* So picture is not right on edge of window */
  282.     
  283.     dispRect.right = picWidth + kTextInset;
  284.     
  285.     DrawPicture( picH, &dispRect );
  286.  
  287.     return;
  288. }
  289.  
  290. short GetCharLine( TEHandle textH, short charNum )
  291. {
  292.     short    lineNum;
  293.     short    *linePtr;
  294.     short    nLines;
  295.     
  296.     linePtr = (*textH)->lineStarts;            /* Warning: Handle deref */
  297.     nLines = (*textH)->nLines;
  298.     
  299.     for ( lineNum = 0; lineNum < nLines; lineNum++ ){
  300.     
  301.         if ( *linePtr++ > charNum ){
  302.             return lineNum + 1;                /* TE uses 1 as the first line */
  303.         }
  304.     }
  305.  
  306.     return lineNum;
  307. }
  308. #endif
  309.